package com.monster.servlet;

import com.alibaba.fastjson.JSONArray;
import com.monster.pojo.Playlist;
import com.monster.pojo.Tag;
import com.monster.service.ListServiceImpl;
import com.monster.service.PlaylistService;
import com.monster.util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/CsxPageServlet")
public class CsxPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PlaylistService playlistService = new ListServiceImpl();
        Playlist playlist = new Playlist();
        Tag tag = new Tag();
        PrintWriter out = response.getWriter();
        String newLable = request.getParameter("lable");
        String currentPage = request.getParameter("pageIndex");
        if(currentPage==null||currentPage.equals("")){
            currentPage="1";
        }
        //当前页面
        int pageIndex = Integer.parseInt(currentPage);
        //每页显示几条
        int pageSize = 8;
        PageSupport pageSupport = new PageSupport();
        //设置当前页面
        pageSupport.setCurrentPageNo(pageIndex);
        //设置每页显示几条
        pageSupport.setPageSize(pageSize);
        //如果标签不为空，就调用查询标签对应的歌单的数量，如果标签为空就调用查询全部歌单数量的方法
        if(newLable!=null&&!(newLable.equals(""))){
            Long lable = Long.parseLong(newLable);
            tag.setTagId(lable);
            int totalCount = playlistService.getTotalCount(tag,null);
            //设置总条数
            pageSupport.setTotalCount(totalCount);
            //设置总页数
            int totalPage = pageSupport.getTotalPageCount();
            if(pageIndex<1){
                pageIndex=totalPage;
            }else if(pageIndex>totalPage){
                pageIndex=1;
            }
            if(pageIndex<1){
                pageIndex=totalPage;
            }else if(pageIndex>totalPage){
                pageIndex=1;
            }
            List<Playlist> playlists = playlistService.playlists(pageIndex,pageSize);
            out.print(JSONArray.toJSON(playlists));
            out.flush();
            out.close();
        }else{
            int totalCount = playlistService.getTotalCount(null,null);
            //设置总条数
            pageSupport.setTotalCount(totalCount);
            //设置总页数
            int totalPage = pageSupport.getTotalPageCount();
            if(pageIndex<1){
                pageIndex=totalPage;
            }else if(pageIndex>totalPage){
                pageIndex=1;
            }
            List<Playlist> playlists = playlistService.playlists(pageIndex,pageSize);
            out.print(JSONArray.toJSON(playlists));
            out.flush();
            out.close();
        }
    }
}
